package com.example.musicplayer;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.MediaStore;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.List;
/**
 * Created by asd on 2016/8/26.
 */
public class LocalMusic extends Activity {
    private ListView lv;
    private List<Music> data;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.local_music);
        local_data();
        init();
    }
    private void local_data() {
        //查询媒体数据库
        ContentResolver resolver = LocalMusic.this.getContentResolver();
        /**
         * Uri：这个Uri代表要查询的数据库名称加上表的名称。
         这个Uri一般都直接从MediaStore里取得，例如我要取所有歌的信息，
         就必须利用MediaStore.Audio.Media. EXTERNAL _CONTENT_URI这个Uri。
         *
         */
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        ergodic(cursor);
    }

    private void init() {


        lv = (ListView) findViewById(R.id.lv_localMusic);
        MyAdapter adapter = new MyAdapter();
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//开启服务
                Intent intent = new Intent(LocalMusic.this, MyServices.class);
                intent.putExtra("pwd","play");
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("music",data.get(position));
                intent.putExtras(mBundle);
                startService(intent);

                //开启MusicShows
                Intent intent1 = new Intent(LocalMusic.this,MusicShows.class);
                    int i = position;
                Log.v("msg",i+"");
                intent1.putExtra("music",i);
//                Bundle mBundle1 = new Bundle();
//                mBundle1.putSerializable("music",position);
//                intent1.putExtras(mBundle1);
                startActivity(intent1);
            }
        });
    }

    private void ergodic(Cursor cursor) {
        data = new ArrayList<>();
//        cursor.moveToFirst();
        while (cursor.moveToNext()) {
            //歌曲文件的路径MediaStore.Audio.Media.DATA
            String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            //歌曲的歌手名MediaStore.Audio.Media.ARTIST
            String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
            //歌曲标题
            String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
            long duration = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DURATION));//时长
            Log.v("mag",duration+"");
            Music music = new Music();
            music.setTime(duration);
            music.setUrl(url);
            music.setTitle(title);
            music.setArtist(artist);
            data.add(music);
        }
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return data.size();
        }
        @Override
        public Object getItem(int position) {
            return null;
        }
        @Override
        public long getItemId(int position) {
            return 0;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(LocalMusic.this, R.layout.music_data, null);
                holder.txt_artist = (TextView) convertView.findViewById(R.id.tv_artist);
                holder.txt_title = (TextView) convertView.findViewById(R.id.tv_title);
                holder.txt_url = (TextView) convertView.findViewById(R.id.tv_url);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.txt_artist.setText(data.get(position).getArtist());
            holder.txt_url.setText(data.get(position).getUrl());
            holder.txt_title.setText(data.get(position).getTitle());
            return convertView;
        }
        private class ViewHolder {
            TextView txt_artist;
            TextView txt_url;
            TextView txt_title;
        }
    }
}
